#!/bin/bash

# 启动Nacos服务器
echo "启动Nacos服务器..."
cargo run -p nacos-server &
SERVER_PID=$!

# 等待服务器启动
sleep 5

# 测试健康检查
echo "测试健康检查..."
curl -f http://localhost:8848/health || echo "健康检查失败"

# 测试服务注册 - 使用查询参数
echo "测试服务注册..."
curl -X POST "http://localhost:8848/nacos/v1/ns/instance?namespace=public&serviceName=test-service&ip=127.0.0.1&port=8080&weight=1.0&healthy=true&enabled=true&ephemeral=true" \
  -H "Content-Type: application/x-www-form-urlencoded" \
  -d "" || echo "服务注册失败"

# 测试获取服务实例列表
echo "测试获取服务实例列表..."
curl "http://localhost:8848/nacos/v1/ns/instance/list?namespace=public&serviceName=test-service" || echo "获取服务实例列表失败"

# 测试服务注销
echo "测试服务注销..."
curl -X DELETE "http://localhost:8848/nacos/v1/ns/instance?namespace=public&serviceName=test-service&ip=127.0.0.1&port=8080" \
  -H "Content-Type: application/x-www-form-urlencoded" \
  -d "" || echo "服务注销失败"

# 测试配置发布
echo "测试配置发布..."
curl -X POST "http://localhost:8848/nacos/v1/cs/configs" \
  -H "Content-Type: application/json" \
  -d '{"namespace":"public","group":"DEFAULT_GROUP","dataId":"test-config","content":"test-content"}' || echo "配置发布失败"

# 测试获取配置
echo "测试获取配置..."
curl "http://localhost:8848/nacos/v1/cs/configs?namespace=public&group=DEFAULT_GROUP&dataId=test-config" || echo "获取配置失败"

# 测试配置删除
echo "测试配置删除..."
curl -X DELETE "http://localhost:8848/nacos/v1/cs/configs" \
  -H "Content-Type: application/json" \
  -d '{"namespace":"public","group":"DEFAULT_GROUP","dataId":"test-config"}' || echo "配置删除失败"

# 停止服务器
echo "停止Nacos服务器..."
kill $SERVER_PID

echo "所有测试完成"